package cn.agiledata.bank.myspace.bean;

import cn.agiledata.bank.audit.service.AuditService;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.ConvertUtil;
import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.common.util.StringUtil;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.AccountBank;
import cn.agiledata.bank.hibernateBean.AuditOperandType;
import cn.agiledata.bank.hibernateBean.BankInfo;
import cn.agiledata.bank.hibernateBean.EsPrePayTrans;
import cn.agiledata.bank.hibernateBean.HistoryAuditOperation;
import cn.agiledata.bank.hibernateBean.Transaction;
import cn.agiledata.bank.transaction.audit.service.EsPrePayTransService;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class PaperBean extends AccountHisQueryDetailBean implements PrintPaperConstant {
	
	
	private String upperRealTrasAmount; //大写实际金额
	private String upperAmount;			//大写交易金额
	private String upperFee;			//大写费用
	private char[] lowerRealTrasAmount; //小写实际金额
	public String[][] person;			//经办人,审核人的显示数组
	public String isPayPerson;			//是否是付款方
	

	public PaperBean(Account a,Transaction t){
		super(a,t);
		setPerson();
	}
	/**
	 * 
	 * @return
	 */
	public AuditService getAuditService() {
		AuditService auditService = (AuditService)IPBAppFactory.getObj("auditService");
		return auditService;
	}
	
	public EsPrePayTransService getEsPrePayTransService() {
		EsPrePayTransService transService = (EsPrePayTransService)IPBAppFactory.getObj("EsPrePayTransService");
		return transService;
	}
	
	/**
	 * 得到大写金额
	 * @return
	 */
	public String getUpperRealTrasAmount() {
		return UPPER_MONEY_PREFIX + ConvertUtil.getUpperMoney(this.getRealTrasAmount());
	}

	public void setUpperRealTrasAmount(String upperRealTrasAmount) {
		this.upperRealTrasAmount = upperRealTrasAmount;
	}
	

	
	public String getUpperAmount() {
		return UPPER_MONEY_PREFIX + ConvertUtil.getUpperMoney(this.getAmount());
	}
	public void setUpperAmount(String upperAmount) {
		this.upperAmount = upperAmount;
	}
	public String getUpperFee() {
		return UPPER_MONEY_PREFIX + ConvertUtil.getUpperMoney(this.getFee());
	}
	public void setUpperFee(String upperFee) {
		this.upperFee = upperFee;
	}
	/**
	 * 
	 * @return
	 */
	public String getIsPayPerson() {
		boolean b = false;
		if(a.getId().longValue() == t.getDebit().getAccount().getId().longValue()) {
			b = true;
		}
		return ConvertUtil.convertFromBooleanToString(b);
	}
	
	public void setIsPayPerson(String isPayPerson) {
		this.isPayPerson = isPayPerson;
	}
	
	/**
	 * 
	 * @return
	 */
	public String[][] getPerson() {
		return person;
	}
	
	public void setPerson() {
		boolean audit = true;
		AuditOperandType auditOperandType = getAuditService().getAuditOperandType(OPERAND_CLASS, t.getActionType().getId().toString());
		HistoryAuditOperation[] auditOperation = null;
		if(auditOperandType == null) {
			audit = false;
		} else {
			EsPrePayTrans payTrans = getEsPrePayTransService().getEsPrePayTransByTransId(getTransId());
			if(payTrans != null) {
				auditOperation = getAuditService().getHistoryAuditOperationsOfLastFlow(auditOperandType,payTrans.getId().longValue());
			}
		
			if(auditOperation == null || (auditOperation != null && auditOperation.length==0)) {
				audit = false;
			}
		}
		
		if(audit) {
			setOperateUserWithAudit(auditOperation);
		} else {
			setOperateUserWithoutAudit();
		}
	}
	
	public void setOperateUserWithoutAudit() {
		String[][] strings = new String[1][3];
		strings[0][0] = "";
		strings[0][1] = "";
		strings[0][2] = "";
		if(t.getOperatorSeq() != null) {
			strings[0][0] = OPERATION_PERSON;
			strings[0][1] = ":";
			strings[0][2] = getMySpaceService().getOperatorById(t.getOperatorSeq()).getOperatorName(); //得到操作员姓名
		}
		setPerson(strings);
	}
	
	public void setOperateUserWithAudit(HistoryAuditOperation[] auditOperation) {
		String[][] strings = new String[auditOperation.length][3];
		if(auditOperation != null) {
			for(int i=0;i<auditOperation.length;i++) {
				HistoryAuditOperation operation = (HistoryAuditOperation)auditOperation[i];
				if(i==0) {
					strings[auditOperation.length -1 - i][0] = OPERATION_PERSON;
				} else {
					strings[auditOperation.length -1 -i][0] = DateUtil.number2Chinese(String.valueOf(i))+AUDIT_PERSON.substring(1);
				}
				strings[auditOperation.length -1 - i][1] = ":";
				strings[auditOperation.length -1 - i][2] = getMySpaceService().getOperatorById(operation.getUpdateUser()) == null ? "":getMySpaceService().getOperatorById(operation.getUpdateUser()).getOperatorName();
			}
		}		
		setPerson(strings);		
	}
	

	

	public void setPerson(String[][] person) {
		this.person = person;
	}
	


	
	public char[] getLowerRealTrasAmount() {
		char[] chars = new char[13];
		String amount = LOWER_MONEY_PREFIX + this.getRealTrasAmount().replaceAll("\\.", "");
		if(amount.length() > 13) {
			return chars;
		} else {
			int length = amount.length();
			for(int i=0;i<length;i++) {
				chars[13-length+i] = amount.charAt(i);
			}
			
			return chars;
		}
	}
	
	public void setLowerRealTrasAmount(char[] lowerRealTrasAmount) {
		this.lowerRealTrasAmount = lowerRealTrasAmount;
	}

	public String getFromAccountName() {
		if(t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_A) ||
		   t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_K) ||
		   t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_O)) {
			
			return ((BankInfo)getBankTemplate().getBankInfo(t.getBankSeq().toString())).getBankName();
		} else if(t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_B)) {		
			if(t.getBankSeq() != null) { //���л��银行汇款
				BankInfo bi = (BankInfo)getBankTemplate().getBankInfo(t.getBankSeq().toString());
				if(bi != null) {
					if(t.getBankName() != null)
						return bi.getBankName()+"("+t.getBankName()+")";	
					else  {
						return bi.getBankName();
					}
				} else {
					return "";
				}
			} else { //邮局汇款
				return "邮局";
			}
		}else if(t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_C) ||
			t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_D)||	
			t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_G)||
			t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_H)) {	
			if(StringUtil.isStrEmpty(t.getDebit().getAccount().getEmail())) {
				return t.getDebit().getAccount().getName();
			} else {
				return t.getDebit().getAccount().getName();
			}
		} else if(t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_F) ||
				  t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_L) ||
				  t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_P) ||
				  t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_Q)) {	
			if(t.getPersonName() != null) {
				return "����:"+t.getPersonName();
			} else {
				return ((BankInfo)getBankTemplate().getBankInfo(t.getBankSeq().toString())).getBankName();
			}
		} else {
			return "";
		}
	}
	
	public String getToAccountName() {
		if(t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_A) ||
		  t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_B) ||	
		   t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_C)||
		   t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_F)||
			t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_H) ||
			t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_X) ||
			t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_Q) ||
			t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_O)) {	//��ֵ,���³�ֵ,����佻��,������Է�����,���и����Է�����,�ڲ��ʻ�ת��
			if(StringUtil.isStrEmpty(t.getCredit().getAccount().getEmail())) {
				return t.getCredit().getAccount().getName();
			} else {
				return t.getCredit().getAccount().getName();
			}
		} else if(t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_D)) {	//����
			if(t.getAccountBankSeq() != null) {
				String accountBankSeq = t.getAccountBankSeq();
				
				AccountBank ab = (AccountBank)getMySpaceService().getAccountBank(accountBankSeq);
				return ab.getAccountName();
			} else {
				if(t.getPersonName() != null) {
					return t.getPersonName();
				} else {
					return "";
				}					
			}
		}	else if(t.getActionType().getId().toString().equals(Constant.ACTION_TYPE_G)) {	//������Է�����
			if(t.getPersonName() != null) {
				return t.getPersonName();
			} else {
				return "";
			}
		} else {
			return "";
		}
	}
	
	public String getToAccountEmail() {
		return WeatherUtil.getEmailOrMobileByAccount(t.getCredit().getAccount());
	}
	
	public String getFromAccountEmail() {
		return WeatherUtil.getEmailOrMobileByAccount(t.getDebit().getAccount());
	}
	
	public String getUpdateDate() {
		return DateUtil.date2String((t.getUpdateTime()), YEAR_FORMAT);
	}

}
